﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using pigs;

namespace Pigs.Enemies
{
    class SteelBoss : pigs.Pig
    {
        Boolean goingUp;// is the big going up or down on the screen
        HealthBar healthBar;
        GraphicsDevice graphics;
        int flameCount;
        public bool flaming;

        public List<Pigs.Weapons.Flame> flames;
        public Texture2D flamesTexture;
        Random rand;

        public SteelBoss(Texture2D texture, Vector2 playerPosition, GraphicsDevice gd, int screenWidth, Texture2D flameTexture)
            : base(texture, playerPosition)
        {
            flameCount = 0;
            flaming = false;
            rand = new Random();

            START_SPEED = 0.75f;
            START_HEALTH = 100;
            this.health = 100;
            this.shouldEatSlop = false;
            this.ID = "steelBoss";
            this.speed = START_SPEED;
            this.isInvincible = true;
            goingUp = true;
            flames = new List<Pigs.Weapons.Flame>();
            this.flamesTexture = flameTexture;

            this.graphics = gd;
            this.healthBar = new HealthBar(Color.Green, this.health, gd, new Vector2(50,50), screenWidth);
        }

        /// <summary>
        /// move to the left until at 400px
        /// after that move up and down
        /// </summary>
        public void Update()
        {
            flameCount--;
            if (position.X < 450 && flaming)
            {
                Pigs.Weapons.Flame f1 = new Pigs.Weapons.Flame(flamesTexture, new Vector2(this.position.X + 10, this.position.Y + this.texture.Height / 2 + 28));
                f1.Initialize(new Vector2(this.position.X - 5000, (this.position.Y + this.texture.Height / 2 + rand.Next(-150, 150))));
                flames.Add(f1);
            }
            if (flameCount <= 0)
            {
                if (flaming)
                {
                    flaming = false;
                    flameCount = rand.Next(30, 100);
                }
                else
                {
                    flaming = true;
                    flameCount = rand.Next(100, 300);
                }
            }

            foreach(Pigs.Weapons.Flame f in flames)
            {
                f.Update();
            }
            for (int i = 0; i < flames.Count; i++)
            {
                //check if a flame has gone off viewing area
                if (!flames[i].active)
                {
                   flames.RemoveAt(i);
                }
            }

            if (this.position.X > 400)
            {
                this.position.X -= START_SPEED;
            }
            else if (goingUp)
            {
                this.position.Y -= START_SPEED;
                if (position.Y < 50)
                {
                    goingUp = false;
                }
            }
            else
            {
                this.position.Y += START_SPEED;
                if (position.Y > 718 - Height)
                {
                    goingUp = true;
                }
            }
            rectangle = new Rectangle((int)position.X, (int)position.Y, Width, Height);
            healthBar.update(graphics, health);
        }

        /// <summary>
        /// draw the boss
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (healthBar.oldHealth > health)
            {
                spriteBatch.Draw(this.texture, this.position, null, Color.Red, 0f, Vector2.Zero, 1f, effect, 0f);
            }
            else
            {
                spriteBatch.Draw(this.texture, this.position, null, Color.White, 0f, Vector2.Zero, 1f, effect, 0f);
            }

            foreach (Pigs.Weapons.Flame f in flames) { f.Draw(spriteBatch); }
            healthBar.Draw(spriteBatch);
        }
    }
}
